package com.zk.controller;


import Util.CreateJwt;
import com.zk.po.User;
import com.zk.service.UserService;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kun
 * @since 2022-06-08
 */
@CrossOrigin(origins = "*", allowedHeaders = "*", maxAge = 3600)
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public void put(String key, String value) {
        //设置过期时间x秒
        redisTemplate.opsForValue().set(key, value, 43200, TimeUnit.SECONDS);
    }

    public String get(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    @RequestMapping("/register")
    @ResponseBody
    boolean addUser(String nickname, String username, String password, String email) {
        return userService.addUser(nickname, username, password, email);
    }

    @RequestMapping("/login")
    @ResponseBody
    User loginUser(String username, String password) {
        try {
            User user = userService.loginUser(username, password);
            String token = new CreateJwt().getoken(user);
            user.setToken(token);
            put(token, user.getId());
            return user;
        } catch (Exception e) {
            return null;
        }

    }

    @RequestMapping("/serachUsername")
    @ResponseBody
    boolean serachUsername(String username) {
        if (userService.findUserByUsername(username) != null) {
            System.out.println("用户已存在:" + userService.findUserByUsername(username));
            return true;
        } else {
            return false;
        }

    }

    @RequestMapping("/serachNickname")
    @ResponseBody
    boolean serachNickname(String nickname) {
        if (userService.findUserByNickname(nickname) != null) {
            System.out.println("用户已存在:" + userService.findUserByNickname(nickname));
            return true;
        } else {
            return false;
        }
    }

    //登出
    @RequestMapping("/loginOut")
    @ResponseBody
    public boolean logout(String token) {
        try {
            delete(token);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    //查询登录状态
    @RequestMapping("/iflogin")
    @ResponseBody
    public String iflogin(String token) {
        try {
            return get(token);
        } catch (Exception e) {
            return null;
        }
    }


    //获取用户数据
    @RequestMapping("/serachUser")
    public User getUser(String id) {
        System.out.println(userService.getUserData(id));
        return userService.getUserData(id);
    }

}

